package com.lashou.chunshen.lashoutuangou.tools;

import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;

/**
 * 百度地圖定位工具
 * Created by Administrator on 2016/8/31.
 */

public class BaiDuLocationTool {
    // 定位相关
    LocationClient mLocClient;
    static BaiDuLocationTool single;
    private  boolean isFirstLoc = true;
    public  MyLocationListenner myListener = new MyLocationListenner();
    // 定位成功返回信息的回调
    private  BdLocationSuccessListenner listenner = null;
    public  void getLocationInfo(BdLocationSuccessListenner listenner) {
        // 定位初始化
        this.listenner = listenner;
        mLocClient = new LocationClient(AppApplication.getInstance());

        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setNeedDeviceDirect(true);// 设置返回结果包含手机的方向
        option.setAddrType("all");// 返回的定位结果包含地址信息
        option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02
        option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.registerLocationListener(myListener);
        mLocClient.start();
    }
    // 静态工厂方法
    public synchronized static BaiDuLocationTool getInstance() {
        if (single == null) {
            single = new BaiDuLocationTool();
        }
        return single;
    }

    /***
     * 实现定位的sdk监听函数
     */
    public  class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) {
                return;
            }
//            MyLocationData locData = new MyLocationData.Builder()
//                    .accuracy(location.getRadius())
//                    // 此处设置开发者获取到的方向信息，顺时针0-360
//                    .direction(100).latitude(location.getLatitude())
//                    .longitude(location.getLongitude()).build();
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                int locType = location.getLocType();
                Log.i("mybaidumap", "当前定位的返回值是：" + locType);
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();
                if (location.hasRadius()) {// 判断是否有定位精度半径
                    float radius = location.getRadius();
                }
                String addrStr = null;
                if (locType == BDLocation.TypeNetWorkLocation) {
                    addrStr = location.getAddrStr();// 获取反地理编码(文字描述的地址)
                    Log.i("mybaidumap", "当前定位的地址是：" + addrStr);
                }
                float direction = location.getDirection();// 获取手机方向，【0~360°】,手机上面正面朝北为0°
                String province = location.getProvince();// 省份
                String city = location.getCity();// 城市
                String district = location.getDistrict();// 区县
                Log.i("mybaidumap", "province是：" + province + " city是" + city + " 区县是: " + district);
                listenner.locationResult(latitude,longitude,addrStr,locType+"");
            }
        }
    }

    public  interface BdLocationSuccessListenner {
        public void locationResult(double _latitude, double _longitude,
                                   String _locationAddr,String _locationType);
    }

}
